import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.Text;

public class SettingsScene {
    private GameManager gameManager;
    private static final int WIDTH = 400;
    private static final int HEIGHT = 600;
    
    private Slider widthSlider;
    private Slider heightSlider;
    private Slider speedSlider;

    public SettingsScene(GameManager gameManager) {
        this.gameManager = gameManager;
    }

    public Scene createScene() {
        VBox root = new VBox(20);
        root.setStyle("-fx-background-color: #222222; -fx-alignment: center;");
        root.setPadding(new javafx.geometry.Insets(50));

        Text title = new Text("游戏设置");
        title.setFont(Font.font("Arial", 30));
        title.setFill(javafx.scene.paint.Color.WHITE);

        GridPane settingsGrid = new GridPane();
        settingsGrid.setHgap(10);
        settingsGrid.setVgap(10);
        settingsGrid.setPadding(new javafx.geometry.Insets(10));

        // 宽度设置
        Label widthLabel = new Label("游戏区域宽度:");
        widthLabel.setTextFill(javafx.scene.paint.Color.WHITE);
        widthSlider = new Slider(8, 16, 10);
        widthSlider.setShowTickLabels(true);
        widthSlider.setShowTickMarks(true);
        widthSlider.setMajorTickUnit(2);
        widthSlider.setBlockIncrement(1);
        
        Label widthValueLabel = new Label();
        widthValueLabel.setTextFill(javafx.scene.paint.Color.WHITE);
        widthSlider.valueProperty().addListener((observable, oldValue, newValue) -> {
            widthValueLabel.setText(String.valueOf(newValue.intValue()));
        });
        widthValueLabel.setText(String.valueOf((int)widthSlider.getValue()));

        // 高度设置
        Label heightLabel = new Label("游戏区域高度:");
        heightLabel.setTextFill(javafx.scene.paint.Color.WHITE);
        heightSlider = new Slider(16, 24, 20);
        heightSlider.setShowTickLabels(true);
        heightSlider.setShowTickMarks(true);
        heightSlider.setMajorTickUnit(2);
        heightSlider.setBlockIncrement(1);
        
        Label heightValueLabel = new Label();
        heightValueLabel.setTextFill(javafx.scene.paint.Color.WHITE);
        heightSlider.valueProperty().addListener((observable, oldValue, newValue) -> {
            heightValueLabel.setText(String.valueOf(newValue.intValue()));
        });
        heightValueLabel.setText(String.valueOf((int)heightSlider.getValue()));

        // 初始速度设置
        Label speedLabel = new Label("初始速度:");
        speedLabel.setTextFill(javafx.scene.paint.Color.WHITE);
        speedSlider = new Slider(1, 10, 5);
        speedSlider.setShowTickLabels(true);
        speedSlider.setShowTickMarks(true);
        speedSlider.setMajorTickUnit(1);
        speedSlider.setBlockIncrement(1);
        
        Label speedValueLabel = new Label();
        speedValueLabel.setTextFill(javafx.scene.paint.Color.WHITE);
        speedSlider.valueProperty().addListener((observable, oldValue, newValue) -> {
            speedValueLabel.setText(String.valueOf(newValue.intValue()));
        });
        speedValueLabel.setText(String.valueOf((int)speedSlider.getValue()));

        // 添加到网格
        settingsGrid.add(widthLabel, 0, 0);
        settingsGrid.add(widthSlider, 1, 0);
        settingsGrid.add(widthValueLabel, 2, 0);
        
        settingsGrid.add(heightLabel, 0, 1);
        settingsGrid.add(heightSlider, 1, 1);
        settingsGrid.add(heightValueLabel, 2, 1);
        
        settingsGrid.add(speedLabel, 0, 2);
        settingsGrid.add(speedSlider, 1, 2);
        settingsGrid.add(speedValueLabel, 2, 2);

        Button applyButton = new Button("应用设置");
        applyButton.setPrefSize(150, 40);
        applyButton.setOnAction(e -> {
            int width = (int)widthSlider.getValue();
            int height = (int)heightSlider.getValue();
            int speed = (int)speedSlider.getValue();
            gameManager.applySettings(width, height, speed);
            gameManager.showMainMenu();
        });

        Button backButton = new Button("返回主菜单");
        backButton.setPrefSize(150, 40);
        backButton.setOnAction(e -> gameManager.showMainMenu());

        root.getChildren().addAll(title, settingsGrid, applyButton, backButton);

        return new Scene(root, WIDTH, HEIGHT);
    }
}
